using Microsoft.EntityFrameworkCore;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Config;
using net_work.Data.DTO.Sys;
using net_work.Data.Entity.Sys;
using net_work.Data.VO.Sys;
using net_work.IRepository.Sys;

namespace net_work.Repository.Sys;

public class CollegeRepository(ApplicationDbContext dbContext) : Repository<College>(dbContext), ICollegeRepository
{
    public async Task<long> InsertAsync(CollegeAddRequest collegeAddRequest)
    {
        var existingCollege =
            await dbContext.Colleges.IgnoreQueryFilters()
                .FirstOrDefaultAsync(x => x.ColleName == collegeAddRequest.ColleName);
        if (existingCollege != null)
        {
            return -1;
        }

        var newCollege = new College
        {
            ColleName = collegeAddRequest.ColleName,
            ColleEstTime = collegeAddRequest.ColleEstTime,
            ColleAddr = collegeAddRequest.ColleAddr,
            ColleDesc = collegeAddRequest.ColleDesc,
            Remark = collegeAddRequest.Remark,
            DeanId = collegeAddRequest.DeanId
        };
        await dbContext.Colleges.AddAsync(newCollege);
        await dbContext.SaveChangesAsync();
        return newCollege.Id;
    }

    public async Task<bool> UpdateAsync(CollegeUpdateRequest collegeUpdateRequest)
    {
        var colleges = await dbContext.Colleges
            .IgnoreQueryFilters()
            .Where(x => x.ColleName == collegeUpdateRequest.ColleName || x.Id == collegeUpdateRequest.Id)
            .ToListAsync();

        var conflictCollege = colleges.FirstOrDefault(x => x.Id != collegeUpdateRequest.Id);
        if (conflictCollege != null)
        {
            return false;
        }

        var existingCollege = colleges.FirstOrDefault(x => x.Id == collegeUpdateRequest.Id);
        if (existingCollege == null)
        {
            return false;
        }

        existingCollege.Remark = collegeUpdateRequest.Remark;
        existingCollege.ColleName = collegeUpdateRequest.ColleName;
        existingCollege.ColleEstTime = collegeUpdateRequest.ColleEstTime;
        existingCollege.ColleAddr = collegeUpdateRequest.ColleAddr;
        existingCollege.ColleDesc = collegeUpdateRequest.ColleDesc;
        existingCollege.DeanId = collegeUpdateRequest.DeanId;

        await dbContext.SaveChangesAsync();
        return true;
    }

    public Task<PagedResult<CollegeResult>> GetCollegePagedAsync(PagedRequest request)
    {
        var query = from college in dbContext.Colleges
            join teacher in dbContext.Teachers on college.DeanId equals teacher.Id into TeacherGroup
            from teacher in TeacherGroup.DefaultIfEmpty()
            join person in dbContext.People on teacher.PsnId equals person.Id into PersonGroup
            from person in PersonGroup.DefaultIfEmpty()
            select new CollegeResult
            {
                Id = college.Id,
                ColleName = college.ColleName,
                ColleEstTime = college.ColleEstTime,
                ColleAddr = college.ColleAddr,
                ColleDesc = college.ColleDesc,
                DeanId = college.DeanId,
                DeanName = person.PsnName,
                Remark = college.Remark,
                UpdatedTime = college.UpdatedTime,
                UpdatedBy = college.UpdatedBy,
                CreatedTime = college.CreatedTime,
                CreatedBy = college.CreatedBy,
                DelFlag = college.DelFlag
            };

        return GetPagedQueryResultAsync(query, request);
    }
}